﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PROJECT_BOMBER.BLL
{
    public static class Collision
    {

        public static void Solid(ElementPhysique element1, ElementPhysique element2)
        {

            if (element1.GetType() == new Character(0, 0).GetType())
            {
                Character element = element1 as Character;

                if (element.Case.Intersects(element2.Case))
                {

                    float spaceXleft = element.Position.X + element.Width - element2.Position.X; //TODO change positioncontrol
                    float spaceXright = element2.Position.X + element2.Width - element.Position.X; //TODO change positioncontrol
                    float spaceYtop = element.Position.Y - element2.Position.Y + element.Height; //TODO change positioncontrol
                    float spaceYbottom = element2.Position.Y - element.Position.Y + element2.Height; //TODO change positioncontrol
                    string cote = "";

                    if (spaceXleft >= 0 && element.Speed.X > 0 && spaceXleft < (uint)spaceYtop && spaceXleft < (uint)spaceYbottom) cote = "left";

                    else if (spaceXright >= 0 && element.Speed.X < 0 && spaceXright < (uint)spaceYtop && spaceXright < (uint)spaceYbottom) cote = "right";

                    else if (spaceYbottom >= 0 && element.Speed.Y < 0 && spaceYbottom < (uint)spaceXleft && spaceYbottom < (uint)spaceXright) cote = "bottom";

                    else if (spaceYtop >= 0 && element.Speed.Y > 0 && spaceYtop < (uint)spaceXleft && spaceYtop < (uint)spaceXright) cote = "top";

                    switch (cote)
                    {
                        case "top":
                            if (element.Speed.Y >= 1800)
                            {
                                element.Dead = true;
                            }
                            element.Speed = new Vector2(element.Speed.X, 0);
                            element.Onground++;
                            element.Jump = 0;
                            element.Position = new Vector2(element.Position.X, element2.Position.Y - element.Height);
                            break;

                        case "bottom":
                            element.Speed = new Vector2(element.Speed.X, 0);
                            element.Position = new Vector2(element.Position.X, element2.Position.Y + element2.Height);
                            break;

                        case "right":
                            element.Speed = new Vector2(0, element.Speed.Y);
                            element.Position = new Vector2(element2.Position.X + element2.Width, element2.Position.Y);
                            break;

                        case "left":
                            element.Speed = new Vector2(0, element.Speed.Y);
                            element.Position = new Vector2(element2.Position.X - element.Width, element2.Position.Y);
                            break;
                    }



                }
            }
            

            
        }

        public static void Rebount(ElementPhysique element1, ElementPhysique element2)
        {

            if (element1.GetType() == new Character(0, 0).GetType())
            {
                Character element = element1 as Character;

                #region Rebount character
                if (element.Case.Intersects(element2.Case))
                {

                    float spaceXleft = element.Position.X + element.Width - element2.Position.X; //TODO change positioncontrol
                    float spaceXright = element2.Position.X + element2.Width - element.Position.X; //TODO change positioncontrol
                    float spaceYtop = element.Position.Y - element2.Position.Y + element.Height; //TODO change positioncontrol
                    float spaceYbottom = element2.Position.Y - element.Position.Y + element2.Height; //TODO change positioncontrol
                    string cote = "";

                    if (spaceXleft >= 0 && element.Speed.X > 0 && spaceXleft < (uint)spaceYtop && spaceXleft < (uint)spaceYbottom) cote = "left";

                    else if (spaceXright >= 0 && element.Speed.X < 0 && spaceXright < (uint)spaceYtop && spaceXright < (uint)spaceYbottom) cote = "right";

                    else if (spaceYbottom >= 0 && element.Speed.Y < 0 && spaceYbottom < (uint)spaceXleft && spaceYbottom < (uint)spaceXright) cote = "bottom";

                    else if (spaceYtop >= 0 && element.Speed.Y > 0 && spaceYtop < (uint)spaceXleft && spaceYtop < (uint)spaceXright) cote = "top";

                    switch (cote)
                    {
                        case "top":
                            if (element.Speed.Y >= 1800)
                            {
                                element.Dead = true;
                            }
                            element.Speed = new Vector2(element.Speed.X, (int)(-element.Speedmax.Y / 5.5));
                            element.Jump = 2;
                            element.Fly = false;
                            element.Position = new Vector2(element2.Position.X, element2.Position.Y - element.Height);
                            break;

                        case "bottom":
                            element.Speed = new Vector2(element.Speed.X, 0);
                            element.Position = new Vector2(element2.Position.X, element2.Position.Y + element2.Height);
                            break;

                        case "right":
                            element.Speed = new Vector2(0, element.Speed.Y);
                            element.Position = new Vector2(element2.Position.X + element2.Width, element2.Position.Y);
                            break;

                        case "left":
                            element.Speed = new Vector2(0, element.Speed.Y);
                            element.Position = new Vector2(element2.Position.X - element.Width, element2.Position.Y);
                            break;
                    }
                }
                #endregion
            }

            
        }

        public static void Air(Character element1, Ventilateur element2)
        {

                if (element1.Case.Intersects(element2.CaseAir) && element1.Fly && element1.Position.Y - element2.Position.Y + element2.HeightAir < 10)
                {
                    element1.Speed = new Microsoft.Xna.Framework.Vector2(element1.Speed.X, 0);
                }
                else if (element1.Case.Intersects(element2.CaseAir) && element1.Fly)
                {
                    element1.Speed = new Microsoft.Xna.Framework.Vector2(element1.Speed.X, element1.Speed.Y - element1.Speedmin.Y * 4);
                }
           
        }

        public static void GroundCollision(ElementMove element, Level element2)
        {

                #region character
                int[] nbpx = element2.CalcIntersect(element);

                double Angle = element2.AnglePX(element);

                bool blockleft = false;
                bool blockright = false;
                bool blocktop = false;
                bool blockbottom = false;

                if (nbpx[0] > 0 && nbpx[3] > 0)//tl
                {
                    blockleft = true;
                }
                if (nbpx[0] > 0 && nbpx[1] > 0) // br
                {
                    blocktop = true;
                }
                if ((nbpx[2] > 0 && nbpx[3] > 0) || (Angle != 0 && Angle < 30) )//tr
                {
                    if (element.Speed.Y >= 1800)
                    {
                        element.Dead = true;
                    }
                    blockbottom = true;
                }
                else
                {
                    blockbottom = false;
                }
                if (nbpx[1] > 0 && nbpx[2] > 0)//bl
                {
                    blockright = true;
                }



                if (blockbottom)
                {
                    if (element.Jump > 0 && element.Speed.Y < 0)
                    {

                    }
                    else
                    {
                        if (Angle < 20 && (Angle > 1 || Angle < -1))
                        {
                            element.Speed = new Microsoft.Xna.Framework.Vector2(element.Speed.X, -(float)(Math.Tan(Angle * (Math.PI / 180)) * (element.Speed.X * (Math.PI / 180))) * 180 / (float)Math.PI);
                        }
                        else
                        {
                            element.Speed = new Microsoft.Xna.Framework.Vector2(element.Speed.X, 0);
                        }
                        element.Onground++;
                        element.ActiveSpeedX = true;
                        element.Jump = 0;

                        //element.Position.Y -= -3 + (nbpx[2] + nbpx[3]) / element.Width;
                        element.Position = new Vector2(element.Position.X, element.Position.Y + 3 - (nbpx[2] + nbpx[3]) / element.Width);
                    }

                }

                if (blocktop && element.Speed.Y < 0)
                {
                    element.Speed = new Microsoft.Xna.Framework.Vector2(element.Speed.X, 0);
                  //  element.Position.Y += (nbpx[1] + nbpx[2]) / element.Width;
                    element.Position = new Vector2(element.Position.X, element.Position.Y + (nbpx[1] + nbpx[2]) / element.Width);
                }
                if (blockleft && element.Speed.X < 0)
                {
                    element.Speed = new Microsoft.Xna.Framework.Vector2(-1, element.Speed.Y);
                    //element.Position.X += -1 +(nbpx[3] + nbpx[0]) / element.Height;
                    element.Position = new Vector2(element.Position.X -1 +(nbpx[3] + nbpx[0]) / element.Height, element.Position.Y);
                    element.JumpWall = true;
                }
                if (blockright && element.Speed.X > 0)
                {
                    element.Speed = new Microsoft.Xna.Framework.Vector2(-1, element.Speed.Y);
                    //element.Position.X -= - 4 + (nbpx[1] + nbpx[2]) / element.Height;
                    element.Position = new Vector2(element.Position.X + 4 - (nbpx[1] + nbpx[2]) / element.Height, element.Position.Y);
                    element.JumpWall = true;
                }

                #endregion   

        }

    }
}
